/*
 * Powered By fanrenqij.com
 * Since 2017 - 2017
 */

package com.fanrenqiji.modules.business.wechatuser.service.impl;

import org.hibernate.criterion.Order;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.google.common.collect.Lists;

import com.fanrenqiji.entity.WeChatUserEntity;
import com.fanrenqiji.modules.business.wechatuser.vo.WeChatUserVO;
import com.fanrenqiji.modules.business.wechatuser.dao.WeChatUserDao;
import com.fanrenqiji.modules.business.wechatuser.service.WeChatUserService;

import org.dozer.DozerBeanMapper;
import org.springframework.stereotype.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import com.fanrenqiji.common.persistence.Page;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;
import org.apache.commons.lang3.StringUtils;

import java.util.List;
/**
 * 微信用户业务逻辑实现
 *
 * @author wentao
 */
@Service(value = "weChatUser")
@Transactional(readOnly = true)
public class WeChatUserServiceImpl implements WeChatUserService{

    @Autowired
    private WeChatUserDao weChatUserDao;

    @Autowired
    DozerBeanMapper beanMapper;

/**
 * 插入
 *
 * @param weChatUser
 * 插入的对象
 * @return 插入数据条数
 */
@Transactional
public WeChatUserVO insert(WeChatUserVO weChatUser){
    WeChatUserEntity weChatUserEntity = new WeChatUserEntity();
    beanMapper.map(weChatUser, weChatUserEntity);
    weChatUserDao.save(weChatUserEntity);
    beanMapper.map(weChatUserEntity, weChatUser);
    return weChatUser;
}


/**
 * 批量插入
 *
 * @param list
 *            数据列表
 * @return 插入数据条数
 */
@Transactional
public void insertList(List<WeChatUserVO>list){
    for (WeChatUserVO weChatUser : list) {
        insert(weChatUser);
    }
}


/**
 * 修改
 *
 * @param weChatUser
 *            修改的数据
 * @return 修改的数据
 */
@Transactional
public WeChatUserVO update(WeChatUserVO weChatUser){
    WeChatUserEntity weChatUserEntity = null;
    if(StringUtils.isNotEmpty(weChatUser.getId())) {
        weChatUserEntity =  weChatUserDao.findOne(Long.parseLong(weChatUser.getId()));
    }
    if(weChatUserEntity==null){
        weChatUserEntity = new WeChatUserEntity();
    }
    beanMapper.map(weChatUser, weChatUserEntity);
    weChatUserDao.save(weChatUserEntity);
    beanMapper.map(weChatUserEntity, weChatUser);
    return weChatUser;
}


/**
 * 删除
 *
 * @param id
 *            数据标识
 * @return 删除的数据条数
 */
@Transactional
public void delete(String id){
    weChatUserDao.deleteById(Long.parseLong(id));
}


/**
 * 查询所有数据
 *
 * @return 数据列表
 */
public List<WeChatUserVO>getAll(){
    List<WeChatUserVO> list  = Lists.newArrayList();
    List<WeChatUserEntity> entities =weChatUserDao.findAllList();
    for (WeChatUserEntity weChatUserEntity : entities) {
        WeChatUserVO weChatUserVO = new WeChatUserVO();
        beanMapper.map(weChatUserEntity, weChatUserVO);
        list.add(weChatUserVO);
    }
    return list;
}


/**
 * 通过条件查询数据列表
 *
 * @param weChatUser
 *            查询条件
 * @return 数据列表
 */
public List<WeChatUserVO>getList(WeChatUserVO weChatUser){
    List<WeChatUserVO> list  = Lists.newArrayList();
    DetachedCriteria dc = weChatUserDao.createDetachedCriteria();
    dc.add(Restrictions.eq(WeChatUserEntity.DEL_FLAG, WeChatUserEntity.DEL_FLAG_NORMAL));
    /*
    dc.createAlias("weChatUser", "weChatUser"); // as

    dc.add(Restrictions.ne("id", 1L)); // !=

    if (StringUtils.isNotEmpty(weChatUser.getName())) {
        dc.add(Restrictions.like("name", "%" + weChatUser.getName() + "%")); //like
    }
    if (StringUtils.isNotEmpty(weChatUser.getName())) {
        dc.add(Restrictions.eq("name", weChatUser.getName()));
    }

	dc.addOrder(Order.asc("office.id")).addOrder(Order.asc("name"));

    */
    List<WeChatUserEntity> entities = weChatUserDao.find(dc);
    for (WeChatUserEntity weChatUserEntity : entities) {
        WeChatUserVO weChatUserVO = new WeChatUserVO();
        beanMapper.map(weChatUserEntity, weChatUserVO);
        list.add(weChatUserVO);
    }
    return list;
}


/**
 * 通过id查询数据
 *
 * @param id
 *            数据标识
 * @return 数据对象
 */
public WeChatUserVO get(String id){
    WeChatUserVO weChatUser = new WeChatUserVO();
    WeChatUserEntity weChatUserEntity = weChatUserDao.findOne(Long.parseLong(id));
    if(weChatUserEntity==null){
        weChatUserEntity = new WeChatUserEntity();
    }
    beanMapper.map( weChatUserEntity,weChatUser);
    return weChatUser;
}


/**
 * 根据条件分页查询
 * @param page
 * @param weChatUser
 * @return
 */
public Page<WeChatUserVO>findWeChatUserByPage(Page<WeChatUserVO>page,WeChatUserVO weChatUser){
    Page<WeChatUserEntity>pageEntity=new Page<WeChatUserEntity>(page.getPageNo(),page.getPageSize());
    DetachedCriteria dc=weChatUserDao.createDetachedCriteria();
    dc.add(Restrictions.eq(WeChatUserEntity.DEL_FLAG,WeChatUserEntity.DEL_FLAG_NORMAL));
    if (StringUtils.isNotEmpty(weChatUser.getName())) {
        dc.add(Restrictions.like("name", "%" + weChatUser.getName() + "%")); //like
    }
    /*
    dc.createAlias("weChatUser", "weChatUser"); // as

    dc.add(Restrictions.ne("id", 1L)); // !=


    if (StringUtils.isNotEmpty(weChatUser.getName())) {
        dc.add(Restrictions.eq("name", weChatUser.getName())); //=
    }

    if (!StringUtils.isNotEmpty(page.getOrderBy())) { //排序
	    dc.addOrder(Order.asc("name"))
		  .addOrder(Order.desc("id"));
	}
    */
    if(!StringUtils.isNotEmpty(page.getOrderBy())){
        dc.addOrder(Order.desc("id"));
    }
    pageEntity=weChatUserDao.find(pageEntity,dc);
    beanMapper.map(pageEntity,page);
    List<WeChatUserVO>list=Lists.newArrayList();
    for(WeChatUserEntity weChatUserEntity:pageEntity.getList()){
        WeChatUserVO weChatUserVO=new WeChatUserVO();
        beanMapper.map(weChatUserEntity,weChatUserVO);
        list.add(weChatUserVO);
    }
    page.setList(list);
    return page;

}

    @Override
    public List<WeChatUserVO> fingwechatuserbuname(String name) {
        return null;
    }


}
